package com.zl.csMall.indexShow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zl.csMall.code.config.token.TokenTriggerAspect;
import com.zl.csMall.basic.busFile.service.BusFileService;
import com.zl.csMall.indexShow.service.ShowCommodityService;
import com.zl.csMall.indexShow.po.ShowCommodity;
import com.zl.csMall.util.Util;
import com.zl.csMall.vo.ReturnModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description: ShowCommodityController
 * @author: ChenNing
 * @date 2025-10-20 15:20
 */
@CrossOrigin(origins = "*")
@Api(tags = "首页展示")
@RestController
@RequestMapping("csMall/indexShow")
public class ShowCommodityController {

    @Autowired
    private ShowCommodityService showCommodityService;
    @Autowired
    private BusFileService busFileService;


    /**
     * @description: 获取首页轮播图信息
     * @author ChenNing
     * @date: 2025-10-20 15:22
     */
    @ApiOperation(value = "查询首页轮播图信息", response = ShowCommodity.class)
    @CrossOrigin("*")
    @GetMapping("car")
    public ReturnModel getCarouselData(){
        LambdaQueryWrapper<ShowCommodity> showCommodityQuery = new LambdaQueryWrapper<>();
        showCommodityQuery.select(ShowCommodity::getId, ShowCommodity::getCommodityId,
                        ShowCommodity::getTitle, ShowCommodity::getContent, ShowCommodity::getShowAttachmentId)
                .eq(ShowCommodity::getIsCar, 1)
                .ne(ShowCommodity::getIsCommodityIndexBackground, 1)
                .orderByAsc(ShowCommodity::getSort);
        List<Map<String, Object>> showCommodityList = showCommodityService.getShowCommodityAll(showCommodityQuery);
        return new ReturnModel(showCommodityList);
    }

    /**
     * @description: 获取首页列表信息
     * @author ChenNing
     * @date: 2025-10-20 15:57
     */
    @ApiOperation(value = "查询首页列表信息", response = ShowCommodity.class)
    @CrossOrigin("*")
    @GetMapping
    public ReturnModel getListData(){
        LambdaQueryWrapper<ShowCommodity> showCommodityQuery = new LambdaQueryWrapper<>();
        showCommodityQuery.select(ShowCommodity::getId, ShowCommodity::getCommodityId, ShowCommodity::getTitle, ShowCommodity::getContent,
                        ShowCommodity::getTag, ShowCommodity::getShowAttachmentId)
                .ne(ShowCommodity::getIsCar, 1)
                .ne(ShowCommodity::getIsCommodityIndexBackground, 1)
                .orderByAsc(ShowCommodity::getSort);
        List<Map<String, Object>> showCommodityList = showCommodityService.getShowCommodityAll(showCommodityQuery);
        return new ReturnModel(showCommodityList);
    }

    /**
     * @description: 添加首页展示内容
     * @author ChenNing
     * @date: 2025-10-20 15:45
     */
    @CrossOrigin("*")
    @ApiOperation(value = "添加首页展示商品 - 商家")
    @TokenTriggerAspect
    @PostMapping
    public ReturnModel addShowCommodity(@RequestHeader(required = false) String token,
                                        @Validated @RequestBody ShowCommodity showCommodity){
        Date date = new Date();
        showCommodity.setId(Util.getUuId());
        showCommodity.setIsCommodityIndexBackground(2);
        showCommodity.setCreateDate(date);
        showCommodity.setUpdateDate(date);
        boolean result = showCommodityService.save(showCommodity);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "首页内容添加失败！");
    }

    /**
     * @description: 修改首页展示内容
     * @author ChenNing
     * @date: 2025-10-20 16:10
     */
    @CrossOrigin("*")
    @ApiOperation(value = "修改首页展示商品 - 商家")
    @TokenTriggerAspect
    @PutMapping
    public ReturnModel updateShowCommodity(@RequestHeader(required = false) String token,
                                           @RequestBody ShowCommodity showCommodity){
        if (showCommodity == null || showCommodity.getId() == 0){
            return new ReturnModel(false, "商品ID不能为空！");
        }
        // 查询原有商品信息，判断附件ID是否一致，不一致则删除原有附件信息
        ShowCommodity oldData = showCommodityService.getById(showCommodity.getId());
        if (oldData == null || oldData.getId() == 0){
            return new ReturnModel(false, "未查询到首页商品对应数据！");
        }
        if (!Objects.equals(oldData.getShowAttachmentId(), showCommodity.getShowAttachmentId())){
            busFileService.deleteByAttachmentId(oldData.getShowAttachmentId());
        }

        showCommodity.setUpdateDate(new Date());
        boolean result = showCommodityService.updateById(showCommodity);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "首页商品信息更新失败！");
    }

    /**
     * @description: 删除首页展示商品
     * @param id 首页展示商品ID
     * @author ChenNing
     * @date: 2025-10-20 16:20
     */
    @CrossOrigin("*")
    @ApiOperation(value = "根据ID删除首页展示数据 - 商家")
    @TokenTriggerAspect
    @DeleteMapping
    public ReturnModel deleteShowCommodity(@RequestHeader(required = false) String token,
                                           @RequestParam("id") Long id){
        if (Util.isNullOrEmpty(id)){
            return new ReturnModel(false, "首页商品ID为空！");
        }
        boolean result = showCommodityService.deleteById(id);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "首页展示商品删除失败！");
    }

    /**
     * @description: 获取商品首页顶部背景轮播图集合
     * @author ChenNing
     * @date: 2025-10-23 15:32
     */
    @CrossOrigin("*")
    @ApiOperation(value = "查询商品首页顶部背景轮播图")
    @GetMapping("commodityBack")
    public ReturnModel getCommodityIndexBackground(){
        List<String> fileUrlList = showCommodityService.getCommodityIndexBackground();
        return new ReturnModel(fileUrlList);
    }

    /**
     * @description: 更新商品首页顶部背景轮播图
     * @author ChenNing
     * @date: 2025-10-22 14:37
     */
    @CrossOrigin("*")
    @ApiOperation(value = "更新商品首页顶部背景轮播图 - 商家")
    @TokenTriggerAspect
    @PostMapping("commodityBack")
    public ReturnModel updateCommodityIndexBackground(@RequestHeader(required = false) String token,
                                                      @RequestParam("files") MultipartFile[] fileArr){
        if (fileArr == null || fileArr.length < 1){
            return new ReturnModel(false, "请选择文件！");
        }
        boolean result = showCommodityService.updateCommodityIndexBackground(fileArr);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "背景图片上传失败！");
    }

}
